package org.example.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.sun.javafx.collections.MappingChange;
import org.example.entity.Product;
import org.example.excel.MaterialExcel;
import org.example.excel.MaterialExcelReadListener;
import org.example.excel.ProductExcel;
import org.example.excel.ProductExcelReadListener;
import org.example.exception.ErrorEnum;
import org.example.exception.ServiceException;
import org.example.mapper.ProductMapper;
import org.example.param.ProductParam;
import org.example.service.MaterialService;
import org.example.service.ProductService;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Override
    public Map<String, Object> save(Product product) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", product.getName());
        if (productMapper.exists(queryWrapper)) {
            throw new ServiceException(ErrorEnum.PRODUCT_EXISTS);
        }
        productMapper.insert(product);
        return success();
    }

    @Override
    public Map<String, Object> page(ProductParam param) {

        IPage<Product> page = new Page<>(param.getCurrent(), param.getSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isNullOrEmpty(param.getProductName())) {
            queryWrapper.like("name", param.getProductName());
        }
        if (param.getLowStock() == null && param.getHighStock() != null) {
            queryWrapper.le("stock", param.getHighStock());
        }
        if (param.getLowStock() != null && param.getHighStock() == null) {
            queryWrapper.ge("stock", param.getLowStock());
        }
        if (param.getLowStock() != null && param.getHighStock() != null) {
            queryWrapper.between("stock", param.getLowStock(), param.getHighStock());
        }
        if (!StringUtils.isNullOrEmpty(param.getBrand())) {
            queryWrapper.like("brand", param.getBrand());
        }
        if (param.getPrice() != null) {
            queryWrapper.le("price", param.getPrice());
        }
        page =  productMapper.selectPage(page, queryWrapper);
        return success(page);
    }

    @Override
    public Map<String, Object> disable(Product product) {
        productMapper.updateById(product);
        return success();
    }
    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public Map<String, Object> importExcel(MultipartFile file) {
        ProductExcelReadListener listener = new ProductExcelReadListener();
        try {
            EasyExcel.read(file.getInputStream(), ProductExcel.class, listener).sheet().doRead();
            List<ProductExcel> list = listener.getList();
            String sql = "insert into product (name, stock,warning_stock,brand,price) values (?,?,?,?,?)";
            jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ProductExcel product = list.get(i);
                    ps.setObject(1, product.getName());
                    ps.setObject(2, product.getStock());
                    ps.setObject(3, product.getWarningStock());
                    ps.setObject(4, product.getBrand());
                    ps.setObject(5, product.getPrice());
                }
                @Override
                public int getBatchSize() {
                    return list.size();
                }
            });
            return success();
        } catch (IOException e) {
            throw new ServiceException(ErrorEnum.EXCEL_IMPORT_ERROR);
        }

    }
}

